package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.utils.DownloadUtil;
import cn.itcast.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/contract")
public class ContractController extends BaseController {
     @Autowired
    private DownloadUtil downloadUtil;
    @Reference
    private ContractService contractService;
     @RequestMapping(value = "/list",name = "展示购销合同列表数据")
     public String list(@RequestParam(name = "page",defaultValue = "1") Integer pageNum , //这个name="page" 不能修改的
                        @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize){
         ContractExample example = new ContractExample();
         //查询购销合同 select* from co_contract where company_id=? and create by = ? order by create_time desc
         ContractExample.Criteria criteria = example.createCriteria();
         criteria.andCompanyIdEqualTo(getCompanyId());
         User user = getUser();
         Integer degree = user.getDegree();
         if (degree==4){
             criteria.andCreateByEqualTo(user.getId());
         }else if (degree==3){
             criteria.andCreateDeptEqualTo(user.getDeptId());
         }else if (degree==2){
             criteria.andCreateDeptLike(user.getDeptId()+"%");
         }
         example.setOrderByClause("create_time desc");
         PageInfo pageInfo = contractService.findAll(example, pageNum, pageSize);
         request.setAttribute("page",pageInfo);
         return "cargo/contract/contract-list";
     }
     @RequestMapping(value = "/toAdd", name="跳转到添加合同页面")
     public String toAdd(){
         return "/cargo/contract/contract-add";
     }
     @RequestMapping(value = "/edit",name = "保存购销合同的方法")
     public String edit(Contract contract){
        if (StringUtils.isEmpty(contract.getId())){
            contract.setId(UUID.randomUUID().toString());
            contract.setState(0);
            contract.setCompanyId(getCompanyId());
            contract.setCompanyName(getCompanyName());
            contract.setCreateTime(new Date());
            contract.setCreateBy(getUser().getId());
            contract.setCreateDept(getUser().getDeptId());
            contractService.save(contract);
        }else {
            contractService.update(contract);
        }
         return "redirect:/cargo/contract/list.do";
     }
    @RequestMapping(value="/toUpdate",name="进入修改合同的页面")
    public String toUpdate(String id){
        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);
        return "/cargo/contract/contract-update";
     }

    @RequestMapping(value="/submit" ,name="提交购销合同方法")
    public String submit(String id){
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(1);
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }
    @RequestMapping(value = "/cancel",name = "取消购销合同")
    public String cancel(String id){
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(0);
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }
        @RequestMapping(value="/delete",name="删除合同")
        public String delete(String id){
               contractService.delete(id);
            return "redirect:/cargo/contract/list.do";
        }
        @RequestMapping(value="/print",name="进入导出出货表页面")
        public String print(){

            return "cargo/print/contract-print";
        }
        @RequestMapping(value="/printExcel",name="导出出货表数据")
        public String printExcel(String inputDate) throws Exception{
     List<ContractProductVo> contractProductVoList=contractService.findContractProductVoByShipTime(inputDate,getCompanyId());
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("POI测试");
            sheet.setColumnWidth(0,4200);
            sheet.setColumnWidth(1,26*256);
            sheet.setColumnWidth(2,16*256);
            sheet.setColumnWidth(3,26*256);
            sheet.setColumnWidth(4,16*256);
            sheet.setColumnWidth(5,16*256);
            sheet.setColumnWidth(6,16*256);
            sheet.setColumnWidth(7,16*256);
            sheet.setColumnWidth(8,16*256);

            Row bigTitleRow = sheet.createRow(0);
            for (int i = 1; i <= 8; i++) {
                bigTitleRow.createCell(i);
            }
            bigTitleRow.setHeightInPoints(36);
            sheet.addMergedRegion(new CellRangeAddress(0,0,1,8));
            Cell cell = bigTitleRow.getCell(1);
            cell.setCellValue(inputDate.replace("-0","年").replace("-","年")+"月份出货表");
            cell.setCellStyle(bigTitle(workbook));
            Row titleRow = sheet.createRow(1);
            String[] titles = new String[]{"客户","合同号","货号","数量","工厂","工厂交期","船期","贸易条款"};
            for (int i = 1; i <= 8; i++) {
                cell = titleRow.createCell(i);
                cell.setCellStyle(title(workbook));
                cell.setCellValue(titles[i-1]);
            }
            int rowIndex=2;
            Row row=null;
            for (ContractProductVo contractProductVo : contractProductVoList) {
                row=sheet.createRow(rowIndex);
//                客户	合同号	货号	数量	工厂	工厂交期	船期	贸易条款
                cell = row.createCell(1);
                cell.setCellValue(contractProductVo.getCustomName());
                cell.setCellStyle(text(workbook));

                cell = row.createCell(2);
                cell.setCellValue(contractProductVo.getContractNo());
                cell.setCellStyle(text(workbook));

                cell = row.createCell(3);
                cell.setCellValue(contractProductVo.getProductNo());
                cell.setCellStyle(text(workbook));

                cell = row.createCell(4);
                cell.setCellValue(contractProductVo.getCnumber());
                cell.setCellStyle(text(workbook));

                cell = row.createCell(5);
                cell.setCellValue(contractProductVo.getFactoryName());
                cell.setCellStyle(text(workbook));
//            工厂交期	船期	贸易条款

                cell = row.createCell(6);
                cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(contractProductVo.getDeliveryPeriod()));
                cell.setCellStyle(text(workbook));

                cell = row.createCell(7);
                cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(contractProductVo.getShipTime()));
                cell.setCellStyle(text(workbook));

                cell = row.createCell(8);
                cell.setCellValue(contractProductVo.getTradeTerms());
                cell.setCellStyle(text(workbook));
                rowIndex++;
            }
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            workbook.write(byteArrayOutputStream);
            downloadUtil.download(byteArrayOutputStream,response,"出货表.xlsx");
            return "cargo/print/contract-print";
        }

    private CellStyle bigTitle(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);     //纵向居中
        style.setBorderTop(BorderStyle.THIN);              //上细线
        style.setBorderBottom(BorderStyle.THIN);            //下细线
        style.setBorderLeft(BorderStyle.THIN);             //左细线
        style.setBorderRight(BorderStyle.THIN);             //右细线
        return style;
    }
    //小标题的样式
    public CellStyle title(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);     //纵向居中
        style.setBorderTop(BorderStyle.THIN);              //上细线
        style.setBorderBottom(BorderStyle.THIN);            //下细线
        style.setBorderLeft(BorderStyle.THIN);             //左细线
        style.setBorderRight(BorderStyle.THIN);             //右细线
        return style;
    }
    //文字样式
    public CellStyle text(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);              //横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);     //纵向居中
        style.setBorderTop(BorderStyle.THIN);              //上细线
        style.setBorderBottom(BorderStyle.THIN);            //下细线
        style.setBorderLeft(BorderStyle.THIN);             //左细线
        style.setBorderRight(BorderStyle.THIN);             //右细线

        return style;
    }
    @RequestMapping(value = "/printExcelWithTemplate",name = "导出出货表方法")
    public void printExcelWithTemplate(String inputDate) throws Exception{ //inputDate "2015-01"  "2015-12"

        List<ContractProductVo>  contractProductVoList = contractService.findContractProductVoByShipTime(inputDate,getCompanyId());

//        String projectRoot = session.getServletContext().getRealPath("");//此方法可以获取项目的根目录
        String filePath = session.getServletContext().getRealPath("/make/xlsprint/tOUTPRODUCT.xlsx");
//        filePath就是模板文件的路径
        FileInputStream inputStream = new FileInputStream(filePath);
//读取用来导出出货表的模板
        Workbook workbook = new XSSFWorkbook(inputStream);
//        获取工作表sheet
        Sheet sheet = workbook.getSheetAt(0);
//        向合并后的单元格中写入一句话：2012年8月份出货表
        Cell cell = sheet.getRow(0).getCell(1);
        cell.setCellValue(inputDate.replace("-0","年").replace("-","年")  +"月份出货表"); //2015-01"把-0换成“年”  "2015-12"  把-换成“年”

//        内容   contractProductVoList
//        获取模板中的8个样式
        CellStyle[] cellStyles = new CellStyle[8];
        Row row = null;
        row = sheet.getRow(2);
        for (int i =1; i <=8; i++) {  //获取模板中第3行的那8个单元格的样式
            cellStyles[i-1] = row.getCell(i).getCellStyle();
        }

        int rowIndex=2;

        for (ContractProductVo contractProductVo : contractProductVoList) {
            row = sheet.createRow(rowIndex);
//            客户	合同号	货号	数量	工厂	工厂交期	船期	贸易条款
            cell = row.createCell(1);
            cell.setCellValue(contractProductVo.getCustomName());
            cell.setCellStyle(cellStyles[0]);

            cell = row.createCell(2);
            cell.setCellValue(contractProductVo.getContractNo());
            cell.setCellStyle(cellStyles[1]);

            cell = row.createCell(3);
            cell.setCellValue(contractProductVo.getProductNo());
            cell.setCellStyle(cellStyles[2]);

            cell = row.createCell(4);
            cell.setCellValue(contractProductVo.getCnumber());
            cell.setCellStyle(cellStyles[3]);

            cell = row.createCell(5);
            cell.setCellValue(contractProductVo.getFactoryName());
            cell.setCellStyle(cellStyles[4]);
//            工厂交期	船期	贸易条款

            cell = row.createCell(6);
            cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(contractProductVo.getDeliveryPeriod()));
            cell.setCellStyle(cellStyles[5]);

            cell = row.createCell(7);
            cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(contractProductVo.getShipTime()));
            cell.setCellStyle(cellStyles[6]);

            cell = row.createCell(8);
            cell.setCellValue(contractProductVo.getTradeTerms());
            cell.setCellStyle(cellStyles[7]);
            rowIndex++;
        }

//        把workbook文件导出
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);  //把workbook的内容写入到缓存流中
        downloadUtil.download(byteArrayOutputStream,response,"出货表.xlsx");

    }

}
